package com.neusoft.elmboot.service.impl;

import com.neusoft.elmboot.mapper.BusinessMapper;
import com.neusoft.elmboot.mapper.CartMapper;
import com.neusoft.elmboot.mapper.FoodMapper;
import com.neusoft.elmboot.mapper.OrderDetailetMapper;
import com.neusoft.elmboot.mapper.OrdersMapper;
import com.neusoft.elmboot.po.Business;
import com.neusoft.elmboot.po.Cart;
import com.neusoft.elmboot.po.Credit;
import com.neusoft.elmboot.po.Food;
import com.neusoft.elmboot.po.OrderDetailet;
import com.neusoft.elmboot.po.Orders;
import com.neusoft.elmboot.service.CreditService;
import com.neusoft.elmboot.service.OrdersService;
import com.neusoft.elmboot.util.CommonUtil;
import com.neusoft.elmboot.util.Lock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {
	@Autowired
	private CartMapper cartMapper;

	@Autowired
	private OrdersMapper ordersMapper;
	@Autowired
	private OrderDetailetMapper orderDetailetMapper;

	@Autowired
	private FoodMapper foodMapper;

	@Autowired
	private BusinessMapper businessMapper;
	
	@Autowired
	private CreditService creditService;

	@Override
	@Transactional
	public int createOrders(Orders orders) {// 只需一行注释就可实现事务管理 @Transactional
		// 1、查询当前用户购物车中当前商家的所有食品
		Cart cart = new Cart();// 封装一下
		cart.setUserId(orders.getUserId());
		cart.setBusinessId(orders.getBusinessId());
		List<Cart> cartList = cartMapper.listCart(cart);

		if (!Lock.EmptyLock(cartList.size())) {
			return 0;// 反馈空订单
		}

		// 2、创建订单（返回生成的订单编号）
		orders.setOrderDate(CommonUtil.getCurrentDate());
		// total
		orders.setOrderTotal(0.0);
		Double rst = 0.0;
		for (Cart c : cartList) {
			int temp = c.getFoodId();
			Double price = foodMapper.getFoodById(temp).getFoodPrice();
			rst += price * c.getQuantity();
		}
		orders.setOrderTotal(rst);

		// daId
		orders.setDaId(1);

		ordersMapper.saveOrders(orders);

		int orderId = orders.getOrderId();// 返回的是获取的主键

		// 3、批量添加订单明细
		// List<OrderDetailet> list = new ArrayList<>();
		for (Cart c : cartList) {
			OrderDetailet od = new OrderDetailet();
			od.setOrderId(orderId);
			od.setFoodId(c.getFoodId());
			od.setQuantity(c.getQuantity());
			// list.add(od);
			orderDetailetMapper.saveOrderDetailetBatch(od);
		}

		// 4、从购物车表中删除相关食品信息
		for (Cart c : cartList) {
			cartMapper.removeCart(c);
		}
		List<OrderDetailet> list = orderDetailetMapper.listOrderDetailetByOrderId(orderId);
		// 把食物“拿出来”
		for (OrderDetailet od : list) {
			int foodId = od.getFoodId();
			int quantity = od.getQuantity();
			Food food = foodMapper.getFoodById(foodId);
			int foodCount = food.getFoodCount() - quantity;
			foodMapper.updateFoodCount(foodCount, foodId);
		}

		//调用积分增加
		Credit credit = new Credit(orders.getUserId(),1,0,null);
		BigDecimal count = new BigDecimal(orders.getOrderTotal());
		creditService.insertCredit(credit, count);
		
		return orderId;
	}

	@Override
	@Transactional
	public Orders getOrdersById(Integer orderId) {
		Orders orders = ordersMapper.getOrdersById(orderId);
		int businessId = orders.getBusinessId();
		Business business = businessMapper.getBusinessById(businessId);
		orders.setBusiness(business);

		List<OrderDetailet> let = orderDetailetMapper.listOrderDetailetByOrderId(orderId);

		for (OrderDetailet o : let) {
			Food food = foodMapper.getFoodById(o.getFoodId());
			o.setFood(food);
		}

		orders.setList(let);
		return orders;
	}

	@Override
	@Transactional
	public List<Orders> listOrdersByUserId(String userId) {
		List<Orders> orders = ordersMapper.listOrdersByUserId(userId);

		for (Orders o : orders) {
			int businessId = o.getBusinessId();
			Business business = businessMapper.getBusinessById(businessId);
			o.setBusiness(business);

			List<OrderDetailet> let = orderDetailetMapper.listOrderDetailetByOrderId(o.getOrderId());

			for (OrderDetailet or : let) {
				Food food = foodMapper.getFoodById(or.getFoodId());
				or.setFood(food);
			}

			o.setList(let);
		}

		return orders;
	}

	@Override
	@Transactional
	public int updateOrder(Orders orders) {
		return ordersMapper.updateOrder(orders);

	}

	@Override
	@Transactional
	public int removeOrders(Integer orderId) {
		// TODO Auto-generated method stub
		// Orders order = ordersMapper.getOrdersById(orderId);
		List<OrderDetailet> list = orderDetailetMapper.listOrderDetailetByOrderId(orderId);
		// 把食物“还回去”
		for (OrderDetailet od : list) {
			int foodId = od.getFoodId();
			int quantity = od.getQuantity();
			Food food = foodMapper.getFoodById(foodId);
			int foodCount = food.getFoodCount() + quantity;
			foodMapper.updateFoodCount(foodCount, foodId);
		}
		return ordersMapper.removeOrders(orderId);
	}
}
